package com.mindata.blockchain.socket.common.listener;

import com.mindata.blockchain.socket.body.votebody.PbftVoteBody;
import com.mindata.blockchain.socket.client.PacketSender;
import com.mindata.blockchain.socket.packet.BlockPacket;
import com.mindata.blockchain.socket.packet.PacketBuilder;
import com.mindata.blockchain.socket.packet.PacketType;
import com.mindata.blockchain.socket.common.event.ViewChangeEvent;
import com.mindata.blockchain.socket.pbft.msg.VoteMsg;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
@Component
public class ViewChangeEventListener {
    @Resource
    private PacketSender packetSender;

    // 区块上链后更改视图()
    @EventListener
    public boolean changeView(ViewChangeEvent changeEvent) {
        VoteMsg voteMsg = (VoteMsg) changeEvent.getSource();
        BlockPacket blockPacket = new PacketBuilder<>().setType(PacketType.PBFT_VOTE).setBody(new
                PbftVoteBody(voteMsg)).build();
        packetSender.sendGroup(blockPacket);
        return true;
    }
}
